package com.wangjiyuan.shiwupai.ui_food.food;

import android.util.Log;

import com.wangjiyuan.shiwupai.bean.first_food.FoodBean;

import java.util.Map;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by QXL on 2016/11/28.
 */

public class FoodPresenter implements FoodContract.IFoodPresenter {
    private FoodContract.IFoodModel iFoodModel;
    private FoodContract.IFoodView iFoodView;

    private Subscription subscription;

    public FoodPresenter(FoodFragment foodFragment){
        this.iFoodView=foodFragment;
        iFoodModel=new FoodModel();
    }
    @Override
    public void getFoodObservable(Map<String, String> foodMap) {
        Observable<FoodBean> foodObservable = iFoodModel.getFoodObservable(foodMap);
        subscription=foodObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FoodBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.e("FoodPresenter","start");
                        iFoodView.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        Log.e("FoodPresenter","completed");
                        iFoodView.hideProgress();
                        subscription.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("FoodPresenter","error");
                        iFoodView.showProgress();
                        iFoodView.hideProgress();
                        subscription.unsubscribe();
                    }

                    @Override
                    public void onNext(FoodBean foodBean) {
                        Log.e("FoodPresenter","next");
                        iFoodView.loadData(foodBean);
                    }
                });
    }
}
